<template>
  <div>
    <!-- 查询结果 -->
    <el-table v-loading="listLoading" :data="list" element-loading-text="正在查询中。。。" border fit highlight-current-row>
      <el-table-column align="center" label="标题" prop="title"/>
      <el-table-column align="center" property="posterUrl" label="封面">
        <template slot-scope="scope">
          <img v-if="scope.row.posterUrl" :src="scope.row.posterUrl" class="el-avatar" width="80">
        </template>
      </el-table-column>
      <el-table-column align="center" label="所属电台" prop="stationName"/>
      <!--      <el-table-column align="center" label="主持人" prop="radioMaster"/>-->
      <!--      <el-table-column align="center" label="关键词" prop="keyword"/>-->
      <el-table-column align="center" label="标签" prop="tags"/>
      <el-table-column align="center" label="时长" prop="length"/>
      <el-table-column align="center" label="开始时间" prop="startTime"/>
      <el-table-column align="center" label="结束时间" prop="endTime"/>
      <!--      <el-table-column align="center" label="meta" prop="meta"/>-->
      <el-table-column align="center" label="处理状态" prop="status">
        <template slot-scope="scope">
          {{ findInList(ClipStatusContants, 'key', scope.row.status, 'value') }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="创建" prop="addTime"/>
      <el-table-column align="center" label="操作" width="240" class-name="small-padding">
        <template slot-scope="scope">
          <el-dropdown class="btn" size="mini">
            <span class="el-dropdown-link">
              <el-button size="mini">更多</el-button>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="handleRedo(scope.row)"> 重剪 </el-dropdown-item>
              <el-dropdown-item @click.native="handleDownload(scope.row)"> 下载 </el-dropdown-item>
              <el-dropdown-item @click.native="handleUpdate(scope.row)"> 更新 </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
          <el-button type="danger" size="mini" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

    <el-dialog :visible.sync="dialogFormVisible" title="剪辑列表" style="width: 100%;" close>
      <el-form ref="dataForm" :model="dataForm" status-icon label-position="left" label-width="120px" style="width: 90%; margin-left:20px;">
        <el-row>
          <el-col :span="12">
            <el-form-item label="标题" prop="title">
              <el-input v-model="dataForm.title"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="封面图" prop="posterUrl">
              <file-uploader v-model="dataForm.posterUrl" type="image" accpet="image/*"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="所属电台" prop="stationName">
              <el-input v-model="dataForm.stationName"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="主持人" prop="hostName">
              <el-input v-model="dataForm.hostName"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="关键词" prop="keywords">
          <el-input v-model="dataForm.keywords" type="textarea"/>
        </el-form-item>
        <el-form-item label="标签" prop="tags">
          <el-input v-model="dataForm.tags" type="textarea"/>
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="时长" prop="length">
              <span>{{ dataForm.length }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="处理状态" prop="status">
              <span>{{ findInList(ClipStatusContants, 'key', dataForm.status, 'value') }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="最早的开始时间" prop="startTime">
              <span>{{ dataForm.startTime }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="最晚的结束时间" prop="endTime">
              <span>{{ dataForm.endTime }}</span>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="meta" prop="meta">
          <span>{{ dataForm.meta }}</span>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <span>{{ dataForm.remark }}</span>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">关闭</el-button>
        <el-button type="primary" @click="updateData">确定</el-button>
      </div>
    </el-dialog>

  </div>
</template>
<script>
import BackToTop from '@/components/BackToTop'
import Pagination from '@/components/Pagination'
import ObjectUtil from '../../../utils/object-util'
import { apiVideoClip as API } from '../../../api/clip/sub'
import Fn from '../../../utils/fn'
import FileUploader from '../../../components/common/FileUploader'
import { ClipStatusContants } from '../../../mixin/MixinContants'
export default {
  name: 'RadioMeterialClipResult',
  components: { FileUploader, BackToTop, Pagination },
  mixins: [ClipStatusContants.mixin],
  props: {
    taskId: {
      type: [String, Number],
      default: undefined
    }
  },
  data() {
    return {
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 99999,
        id: undefined,
        tag: undefined,
        title: undefined,
        sort: 'add_time',
        order: 'desc'
      },
      dataForm: {},
      dialogFormVisible: false,
      downloadLoading: false
    }
  },
  created() {
    if (ObjectUtil.notNull(this.taskId)) {
      this.Init()
    }
  },
  methods: {
    Init() {
      this.getList()
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    getList() {
      const api = API.list
      this.listLoading = true
      api({ ...this.listQuery, taskId: this.taskId }).then(res => {
        const resList = res.data.data.list || []
        this.list = resList.filter(t => ObjectUtil.notNull(t))
      }).finally(() => {
        this.listLoading = false
      })
    },
    handleUpdate(row) {
      this.dataForm = Object.assign({}, row)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          API.update(this.dataForm)
            .then(() => {
              for (const v of this.list) {
                if (v.id === this.dataForm.id) {
                  const index = this.list.indexOf(v)
                  this.list.splice(index, 1, this.dataForm)
                  break
                }
              }
              this.dialogFormVisible = false
              this.$notify.success({
                title: '成功',
                message: '更新成功'
              })
            })
            .catch(response => {
              this.$notify.error({
                title: '失败',
                message: (response.data || {})['errmsg'] || '请联系系统管理员'
              })
            })
        }
      })
    },
    handleDelete: Fn.Confirm(function(row) {
      API.delete(row)
        .then(response => {
          this.$notify.success({
            title: '成功',
            message: '删除成功'
          })
          const index = this.list.indexOf(row)
          this.list.splice(index, 1)
        })
        .catch(response => {
          this.$notify.error({
            title: '失败',
            message: (response.data || {})['errmsg'] || '请联系系统管理员'
          })
        })
    }),
    handleRedo(row) {
      API.redo({ id: row.id })
        .then(response => {
          row.status = this.ClipStatusContants.wait.key
          this.$notify.success({
            title: '成功',
            message: '重新剪辑中'
          })
        })
        .catch(response => {
          this.$notify.error({
            title: '失败',
            message: (response.data || {})['errmsg'] || '请联系系统管理员'
          })
        })
    },
    handleDownload(row) {
      if (ObjectUtil.isNull(row.meterialDown)) {
        this.$notify.warning({
          title: '警告',
          message: '剪辑下载地址没有生成，无法下载'
        })
        return
      }
      const a = document.createElement('a')
      a.href = row.meterialDown
      a.download = row.meterialDown
      a.target = '_blank'
      a.click()
    }
  }
}
</script>

<style scoped>

</style>
